//
//  MeCircleViewController.m
//  CarUser
//
//  Created by 王森林 on 2019/1/23.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "MeCircleViewController.h"
#import "MeCircleCell.h"
#import "CarcircleDetailViewController.h"
@interface MeCircleViewController ()
@property (nonatomic, strong) NSMutableArray *topicAry;
@end

@implementation MeCircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title  = @"车友圈";
    [self createUI];
    [self fetchDataFromNet];
    self.mainTableView.mj_header = [Custom_Mjfresh mj_customMjfreshHeaderSetting:^{
        [self fetchDataFromNet];
    }];
    // Do any additional setup after loading the view.
}
-(void)fetchDataFromNet
{
    //   车友圈猎豹
    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"type"],
                             @"TOKEN":user.TOKEN,
                             @"USERNAME":user.mobile
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_30 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
         [self.mainTableView.mj_header endRefreshing];
         NSDictionary *resultDict  = response;
        NSArray *ary = [resultDict objectForKey:@"list"];
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0) {
            self.topicAry = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
            [self hideNoDataView];
        } else {
            
              [self showNodataView:self.mainTableView.centerY-150 withView:self.mainTableView];
        }
      
         [self.mainTableView  reloadData];
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
             [self.mainTableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
}

- (void)createUI
{
    self.mainTableView.frame  = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT-(IS_IPHONE_X?88:64));
    [self.mainTableView registerClass:[MeCircleCell class] forCellReuseIdentifier:@"MeCircleCell"];
    
}

#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.topicAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeCircleCell *cell = (MeCircleCell *)[tableView dequeueReusableCellWithIdentifier:@"MeCircleCell" forIndexPath:indexPath];
 
    [cell setData:self.topicAry[indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.topicAry[indexPath.row];
    CarcircleDetailViewController *controller = [[CarcircleDetailViewController alloc] init];
    controller.ID = [dict objectForKey:@"TALK_ID"];
    [self.navigationController pushViewController:controller animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
